home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / hplip / setup.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-28  |  22.3 KB  |  724 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __version__ = '9.0'
  5. __title__ = 'Printer/Fax Setup Utility'
  6. __mod__ = 'hp-setup'
  7. __doc__ = 'Installs HPLIP printers and faxes in the CUPS spooler. Tries to automatically determine the correct PPD file to use. Allows the printing of a testpage. Performs basic fax parameter setup.'
  8. import sys
  9. import getopt
  10. import time
  11. import os.path as os
  12. import re
  13. import os
  14. import gzip
  15.  
  16. try:
  17.     import readline
  18. except ImportError:
  19.     pass
  20.  
  21. from base.g import *
  22. from base import device, utils, tui, models, module
  23. from prnt import cups
  24. pm = None
  25.  
  26. def plugin_download_callback(c, s, t):
  27.     pm.update(int(100 * c * s / t), utils.format_bytes(c * s))
  28.  
  29. nickname_pat = re.compile('\\*NickName:\\s*\\"(.*)"', re.MULTILINE)
  30. USAGE = [
  31.     (__doc__, '', 'name', True),
  32.     ('Usage: %s [MODE] [OPTIONS] [SERIAL NO.|USB bus:device|IP|DEVNODE]' % __mod__, '', 'summary', True),
  33.     utils.USAGE_MODE,
  34.     utils.USAGE_GUI_MODE,
  35.     utils.USAGE_INTERACTIVE_MODE,
  36.     utils.USAGE_SPACE,
  37.     utils.USAGE_OPTIONS,
  38.     ('Automatic mode:', '-a or --auto (-i mode only)', 'option', False),
  39.     ('To specify the port on a multi-port JetDirect:', '--port=<port> (Valid values are 1\\*, 2, and 3. \\*default)', 'option', False),
  40.     ('No testpage in automatic mode:', '-x (-i mode only)', 'option', False),
  41.     ('To specify a CUPS printer queue name:', '-p<printer> or --printer=<printer> (-i mode only)', 'option', False),
  42.     ('To specify a CUPS fax queue name:', '-f<fax> or --fax=<fax> (-i mode only)', 'option', False),
  43.     ('Type of queue(s) to install:', '-t<typelist> or --type=<typelist>. <typelist>: print*, fax\\* (\\*default) (-i mode only)', 'option', False),
  44.     ('To specify the device URI to install:', '-d<device> or --device=<device> (--qt4 mode only)', 'option', False),
  45.     ('Remove printers or faxes instead of setting-up:', '-r or --rm or --remove (-u only)', 'option', False),
  46.     utils.USAGE_LANGUAGE,
  47.     utils.USAGE_LOGGING1,
  48.     utils.USAGE_LOGGING2,
  49.     utils.USAGE_LOGGING3,
  50.     utils.USAGE_HELP,
  51.     ('[SERIAL NO.|USB ID|IP|DEVNODE]', '', 'heading', False),
  52.     ('USB bus:device (usb only):', '"xxx:yyy" where \'xxx\' is the USB bus and \'yyy\' is the USB device. (Note: The \':\' and all leading zeros must be present.)', 'option', False),
  53.     ('', "Use the 'lsusb' command to obtain this information.", 'option', False),
  54.     ('IPs (network only):', 'IPv4 address "a.b.c.d" or "hostname"', 'option', False),
  55.     ('DEVNODE (parallel only):', '"/dev/parportX", X=0,1,2,...', 'option', False),
  56.     ('SERIAL NO. (usb and parallel only):', '"serial no."', 'option', True),
  57.     utils.USAGE_EXAMPLES,
  58.     ('Setup using GUI mode:', '$ hp-setup', 'example', False),
  59.     ('Setup using GUI mode, specifying usb:', '$ hp-setup -b usb', 'example', False),
  60.     ('Setup using GUI mode, specifying an IP:', '$ hp-setup 192.168.0.101', 'example', False),
  61.     ('One USB printer attached, automatic:', '$ hp-setup -i -a', 'example', False),
  62.     ('USB, IDs specified:', '$ hp-setup -i 001:002', 'example', False),
  63.     ('Network:', '$ hp-setup -i 66.35.250.209', 'example', False),
  64.     ('Network, Jetdirect port 2:', '$ hp-setup -i --port=2 66.35.250.209', 'example', False),
  65.     ('Parallel:', '$ hp-setup -i /dev/parport0', 'example', False),
  66.     ('USB or parallel, using serial number:', '$ hp-setup -i US12345678A', 'example', False),
  67.     ('USB, automatic:', '$ hp-setup -i --auto 001:002', 'example', False),
  68.     ('Parallel, automatic, no testpage:', '$ hp-setup -i -a -x /dev/parport0', 'example', False),
  69.     ('Parallel, choose device:', '$ hp-setup -i -b par', 'example', False),
  70.     utils.USAGE_SPACE,
  71.     utils.USAGE_NOTES,
  72.     ('1. If no serial number, USB ID, IP, or device node is specified, the USB and parallel busses will be probed for devices.', '', 'note', False),
  73.     ("2. Using 'lsusb' to obtain USB IDs: (example)", '', 'note', False),
  74.     ('   $ lsusb', '', 'note', False),
  75.     ('         Bus 003 Device 011: ID 03f0:c202 Hewlett-Packard', '', 'note', False),
  76.     ('   $ hp-setup --auto 003:011', '', 'note', False),
  77.     ("   (Note: You may have to run 'lsusb' from /sbin or another location. Use '$ locate lsusb' to determine this.)", '', 'note', True),
  78.     ('3. Parameters -a, -f, -p, or -t are not valid in GUI (-u) mode.', '', 'note', True),
  79.     utils.USAGE_SPACE,
  80.     utils.USAGE_SEEALSO,
  81.     ('hp-makeuri', '', 'seealso', False),
  82.     ('hp-probe', '', 'seealso', False)]
  83.  
  84. def showPasswordUI(prompt):
  85.     import getpass
  86.     print ''
  87.     print log.bold(prompt)
  88.     username = raw_input('Username: ')
  89.     password = getpass.getpass('Password: ')
  90.     return (username, password)
  91.  
  92.  
  93. def restart_cups():
  94.     if os.path.exists('/etc/init.d/cups'):
  95.         return '/etc/init.d/cups restart'
  96.     if os.path.exists('/etc/init.d/cupsys'):
  97.         return '/etc/init.d/cupsys restart'
  98.     return 'killall -HUP cupsd'
  99.  
  100. mod = module.Module(__mod__, __title__, __version__, __doc__, USAGE, (INTERACTIVE_MODE, GUI_MODE), (UI_TOOLKIT_QT3, UI_TOOLKIT_QT4), run_as_root_ok = True)
  101. (opts, device_uri, printer_name, mode, ui_toolkit, loc) = mod.parseStdOpts('axp:P:f:t:b:d:r', [
  102.     'ttl=',
  103.     'filter=',
  104.     'search=',
  105.     'find=',
  106.     'method=',
  107.     'time-out=',
  108.     'timeout=',
  109.     'printer=',
  110.     'fax=',
  111.     'type=',
  112.     'port=',
  113.     'auto',
  114.     'device=',
  115.     'rm',
  116.     'remove'], handle_device_printer = False)
  117. printer_name = None
  118. fax_name = None
  119. bus = None
  120. setup_print = True
  121. setup_fax = True
  122. makeuri = None
  123. auto = False
  124. testpage_in_auto_mode = True
  125. jd_port = 1
  126. remove = False
  127. for o, a in opts:
  128.     if o == '-x':
  129.         testpage_in_auto_mode = False
  130.         continue
  131.     if o in ('-P', '-p', '--printer'):
  132.         printer_name = a
  133.         continue
  134.     if o in ('-f', '--fax'):
  135.         fax_name = a
  136.         continue
  137.     if o in ('-d', '--device'):
  138.         device_uri = a
  139.         continue
  140.     if o in ('-b', '--bus'):
  141.         bus = [ x.lower().strip() for x in a.split(',') ]
  142.         if not device.validateBusList(bus, False):
  143.             mod.usage(error_msg = [
  144.                 'Invalid bus name'])
  145.         
  146.     device.validateBusList(bus, False)
  147.     if o in ('-t', '--type'):
  148.         setup_fax = False
  149.         setup_print = False
  150.         a = a.strip().lower()
  151.         for aa in a.split(','):
  152.             if aa.strip() == 'print':
  153.                 setup_print = True
  154.                 continue
  155.             None if aa.strip() not in ('print', 'fax') else []
  156.             if aa.strip() == 'fax':
  157.                 if not prop.fax_build:
  158.                     log.error('Cannot enable fax setup - HPLIP not built with fax enabled.')
  159.                 else:
  160.                     setup_fax = True
  161.             prop.fax_build
  162.         
  163.     if o == '--port':
  164.         
  165.         try:
  166.             jd_port = int(a)
  167.         except ValueError:
  168.             mod.usage(error_msg = [
  169.                 'Invalid port number. Must be between 1 and 3 inclusive.'])
  170.         except:
  171.             None<EXCEPTION MATCH>ValueError
  172.         
  173.  
  174.     None<EXCEPTION MATCH>ValueError
  175.     if o in ('-a', '--auto'):
  176.         auto = True
  177.         continue
  178.     if o in ('-r', '--rm', '--remove'):
  179.         remove = True
  180.         continue
  181.  
  182.  
  183. try:
  184.     param = mod.args[0]
  185. except IndexError:
  186.     param = ''
  187.  
  188. log.debug('param=%s' % param)
  189. if mode == GUI_MODE:
  190.     if ui_toolkit == 'qt3':
  191.         if not utils.canEnterGUIMode():
  192.             log.error('%s requires GUI support (try running with --qt4). Also, try using interactive (-i) mode.' % __mod__)
  193.             sys.exit(1)
  194.         
  195.     elif not utils.canEnterGUIMode4():
  196.         log.error('%s requires GUI support (try running with --qt3). Also, try using interactive (-i) mode.' % __mod__)
  197.         sys.exit(1)
  198.     
  199.  
  200. if mode == GUI_MODE:
  201.     pass
  202. None if ui_toolkit == 'qt3' else None<EXCEPTION MATCH>KeyboardInterrupt
  203.  
  204. try:
  205.     cups.setPasswordCallback(showPasswordUI)
  206.     if remove:
  207.         log.error('-r/--rm/--remove not supported in -i mode.')
  208.         sys.exit(1)
  209.     
  210.     if not auto:
  211.         log.info("(Note: Defaults for each question are maked with a '*'. Press <enter> to accept the default.)")
  212.         log.info('')
  213.     
  214.     if param:
  215.         (device_uri, sane_uri, fax_uri) = device.makeURI(param, jd_port)
  216.     
  217.     if not device_uri and bus is None:
  218.         bus = tui.connection_table()
  219.         if bus is None:
  220.             sys.exit(0)
  221.         
  222.         log.info('\nUsing connection type: %s' % bus[0])
  223.         log.info('')
  224.     
  225.     if not device_uri:
  226.         device_uri = mod.getDeviceUri(device_uri, devices = device.probeDevices(bus))
  227.     
  228.     log.info(log.bold('\nSetting up device: %s\n' % device_uri))
  229.     log.info('')
  230.     print_uri = device_uri.replace('hpfax:', 'hp:')
  231.     fax_uri = device_uri.replace('hp:', 'hpfax:')
  232.     (back_end, is_hp, bus, model, serial, dev_file, host, zc, port) = device.parseDeviceURI(device_uri)
  233.     log.debug('Model=%s' % model)
  234.     mq = device.queryModelByURI(device_uri)
  235.     if not mq or mq.get('support-type', SUPPORT_TYPE_NONE) == SUPPORT_TYPE_NONE:
  236.         log.error('Unsupported printer model.')
  237.         sys.exit(1)
  238.     
  239.     if mq.get('fax-type', FAX_TYPE_NONE) in (FAX_TYPE_NONE, FAX_TYPE_NOT_SUPPORTED) and setup_fax:
  240.         setup_fax = False
  241.     
  242.     norm_model = models.normalizeModelName(model).lower()
  243.     plugin = mq.get('plugin', PLUGIN_NONE)
  244.     plugin_installed = utils.to_bool(sys_state.get('plugin', 'installed', '0'))
  245.     if plugin > PLUGIN_NONE and not plugin_installed:
  246.         tui.header('PLUG-IN INSTALLATION')
  247.         hp_plugin = utils.which('hp-plugin')
  248.         if hp_plugin:
  249.             if prop.gui_build:
  250.                 os.system('hp-plugin -i')
  251.             else:
  252.                 os.system('hp-plugin')
  253.         
  254.     
  255.     ppds = cups.getSystemPPDs()
  256.     default_model = utils.xstrip(model.replace('series', '').replace('Series', ''), '_')
  257.     stripped_model = cups.stripModel2(default_model)
  258.     installed_print_devices = device.getSupportedCUPSDevices([
  259.         'hp'])
  260.     for d in installed_print_devices.keys():
  261.         for p in installed_print_devices[d]:
  262.             log.debug("found print queue '%s'" % p)
  263.         
  264.     
  265.     installed_fax_devices = device.getSupportedCUPSDevices([
  266.         'hpfax'])
  267.     for d in installed_fax_devices.keys():
  268.         for f in installed_fax_devices[d]:
  269.             log.debug("found fax queue '%s'" % f)
  270.         
  271.     
  272.     if setup_print:
  273.         tui.header('PRINT QUEUE SETUP')
  274.         if not auto and print_uri in installed_print_devices:
  275.             log.warning('One or more print queues already exist for this device: %s.' % ', '.join(installed_print_devices[print_uri]))
  276.             (ok, setup_print) = tui.enter_yes_no('\nWould you like to install another print queue for this device', 'n')
  277.             if not ok:
  278.                 sys.exit(0)
  279.             
  280.         
  281.     
  282.     if setup_print:
  283.         if auto:
  284.             printer_name = default_model
  285.         
  286.         printer_default_model = default_model
  287.         if device_uri in installed_print_devices and printer_default_model in installed_print_devices[device_uri]:
  288.             i = 2
  289.             while True:
  290.                 t = printer_default_model + '_%d' % i
  291.                 if t not in installed_print_devices[device_uri]:
  292.                     printer_default_model += '_%d' % i
  293.                     break
  294.                 
  295.                 i += 1
  296.         
  297.         if not auto:
  298.             if printer_name is None:
  299.                 while True:
  300.                     printer_name = raw_input(log.bold("\nPlease enter a name for this print queue (m=use model name:'%s'*, q=quit) ?" % printer_default_model))
  301.                     if printer_name.lower().strip() == 'q':
  302.                         log.info('OK, done.')
  303.                         sys.exit(0)
  304.                     
  305.                     if not printer_name or printer_name.lower().strip() == 'm':
  306.                         printer_name = printer_default_model
  307.                     
  308.                     name_ok = True
  309.                     for d in installed_print_devices.keys():
  310.                         for p in installed_print_devices[d]:
  311.                             if printer_name == p:
  312.                                 log.error('A print queue with that name already exists. Please enter a different name.')
  313.                                 name_ok = False
  314.                                 break
  315.                                 continue
  316.                         
  317.                     
  318.                     for d in installed_fax_devices.keys():
  319.                         for f in installed_fax_devices[d]:
  320.                             if printer_name == f:
  321.                                 log.error('A fax queue with that name already exists. Please enter a different name.')
  322.                                 name_ok = False
  323.                                 break
  324.                                 continue
  325.                         
  326.                     
  327.                     for c in printer_name:
  328.                         if c in cups.INVALID_PRINTER_NAME_CHARS:
  329.                             log.error("Invalid character '%s' in printer name. Please enter a name that does not contain this character." % c)
  330.                             name_ok = False
  331.                             continue
  332.                     
  333.                     if name_ok:
  334.                         break
  335.                         continue
  336.             
  337.         else:
  338.             printer_name = printer_default_model
  339.         log.info('Using queue name: %s' % printer_name)
  340.         default_model = utils.xstrip(model.replace('series', '').replace('Series', ''), '_')
  341.         stripped_model = default_model.lower().replace('hp-', '').replace('hp_', '')
  342.         log.info('Locating PPD file... Please wait.')
  343.         print_ppd = cups.getPPDFile2(stripped_model, ppds)
  344.         enter_ppd = False
  345.         if print_ppd is None:
  346.             enter_ppd = True
  347.             log.error('Unable to find an appropriate PPD file.')
  348.         else:
  349.             (print_ppd, desc) = print_ppd
  350.             log.info('\nFound PPD file: %s' % print_ppd)
  351.             log.info('Description: %s' % desc)
  352.             if not auto:
  353.                 log.info('\nNote: The model number may vary slightly from the actual model number on the device.')
  354.                 (ok, ans) = tui.enter_yes_no('\nDoes this PPD file appear to be the correct one')
  355.                 if not ok:
  356.                     sys.exit(0)
  357.                 
  358.                 if not ans:
  359.                     enter_ppd = True
  360.                 
  361.             
  362.         if enter_ppd:
  363.             enter_ppd = False
  364.             (ok, enter_ppd) = tui.enter_yes_no('\nWould you like to specify the path to the correct PPD file to use', 'n')
  365.             if not ok:
  366.                 sys.exit(0)
  367.             
  368.             if enter_ppd:
  369.                 ok = False
  370.                 while True:
  371.                     user_input = raw_input(log.bold('\nPlease enter the full filesystem path to the PPD file to use (q=quit) :'))
  372.                     if user_input.lower().strip() == 'q':
  373.                         log.info('OK, done.')
  374.                         sys.exit(0)
  375.                     
  376.                     file_path = user_input
  377.                     if os.path.exists(file_path) and os.path.isfile(file_path):
  378.                         if file_path.endswith('.gz'):
  379.                             nickname = gzip.GzipFile(file_path, 'r').read(4096)
  380.                         else:
  381.                             nickname = file(file_path, 'r').read(4096)
  382.                         
  383.                         try:
  384.                             desc = nickname_pat.search(nickname).group(1)
  385.                         except AttributeError:
  386.                             desc = ''
  387.  
  388.                         if desc:
  389.                             log.info('Description for the file: %s' % desc)
  390.                         else:
  391.                             log.error("No PPD 'NickName' found. This file may not be a valid PPD file.")
  392.                         (ok, ans) = tui.enter_yes_no('\nUse this file')
  393.                         if not ok:
  394.                             sys.exit(0)
  395.                         
  396.                         if ans:
  397.                             print_ppd = file_path
  398.                         
  399.                     else:
  400.                         log.error('File not found or not an appropriate (PPD) file.')
  401.                     if ok:
  402.                         break
  403.                         continue
  404.             else:
  405.                 log.error('PPD file required. Setup cannot continue. Exiting.')
  406.                 sys.exit(1)
  407.         
  408.         if auto:
  409.             (location, info) = ('', 'Automatically setup by HPLIP')
  410.         else:
  411.             while True:
  412.                 location = raw_input(log.bold('Enter a location description for this printer (q=quit) ?'))
  413.                 if location.strip().lower() == 'q':
  414.                     log.info('OK, done.')
  415.                     sys.exit(0)
  416.                 
  417.                 break
  418.             while True:
  419.                 info = raw_input(log.bold('Enter additonal information or notes for this printer (q=quit) ?'))
  420.                 if info.strip().lower() == 'q':
  421.                     log.info('OK, done.')
  422.                     sys.exit(0)
  423.                 
  424.                 break
  425.         log.info(log.bold('\nAdding print queue to CUPS:'))
  426.         log.info('Device URI: %s' % print_uri)
  427.         log.info('Queue name: %s' % printer_name)
  428.         log.info('PPD file: %s' % print_ppd)
  429.         log.info('Location: %s' % location)
  430.         log.info('Information: %s' % info)
  431.         log.debug('Restarting CUPS...')
  432.         (status, output) = utils.run(restart_cups())
  433.         log.debug('Restart CUPS returned: exit=%d output=%s' % (status, output))
  434.         cups.setPasswordPrompt('You do not have permission to add a printer.')
  435.         if not os.path.exists(print_ppd):
  436.             (status, status_str) = cups.addPrinter(printer_name.encode('utf8'), print_uri, location, '', print_ppd, info)
  437.         else:
  438.             (status, status_str) = cups.addPrinter(printer_name.encode('utf8'), print_uri, location, print_ppd, '', info)
  439.         log.debug('addPrinter() returned (%d, %s)' % (status, status_str))
  440.         installed_print_devices = device.getSupportedCUPSDevices([
  441.             'hp'])
  442.         if print_uri not in installed_print_devices or printer_name not in installed_print_devices[print_uri]:
  443.             log.error('Printer queue setup failed. Please restart CUPS and try again.')
  444.             sys.exit(1)
  445.         
  446.     
  447.     if setup_fax and not (prop.fax_build):
  448.         log.error('Cannot setup fax - HPLIP not built with fax enabled.')
  449.         setup_fax = False
  450.     
  451.     if setup_fax:
  452.         
  453.         try:
  454.             from fax import fax
  455.         except ImportError:
  456.             setup_fax = False
  457.             log.warning('Fax setup disabled - Python 2.3+ required.')
  458.         except:
  459.             None<EXCEPTION MATCH>ImportError
  460.         
  461.  
  462.     None<EXCEPTION MATCH>ImportError
  463.     log.info('')
  464.     if setup_fax:
  465.         tui.header('FAX QUEUE SETUP')
  466.         if not auto and fax_uri in installed_fax_devices:
  467.             log.warning('One or more fax queues already exist for this device: %s.' % ', '.join(installed_fax_devices[fax_uri]))
  468.             (ok, setup_fax) = tui.enter_yes_no('\nWould you like to install another fax queue for this device', 'n')
  469.             if not ok:
  470.                 sys.exit(0)
  471.             
  472.         
  473.     
  474.     if setup_fax:
  475.         if auto:
  476.             fax_name = default_model + '_fax'
  477.         
  478.         fax_default_model = default_model + '_fax'
  479.         if fax_uri in installed_fax_devices and fax_default_model in installed_fax_devices[fax_uri]:
  480.             i = 2
  481.             while True:
  482.                 t = fax_default_model + '_%d' % i
  483.                 if t not in installed_fax_devices[fax_uri]:
  484.                     fax_default_model += '_%d' % i
  485.                     break
  486.                 
  487.                 i += 1
  488.         
  489.         if not auto:
  490.             if fax_name is None:
  491.                 while True:
  492.                     fax_name = raw_input(log.bold("\nPlease enter a name for this fax queue (m=use model name:'%s'*, q=quit) ?" % fax_default_model))
  493.                     if fax_name.lower().strip() == 'q':
  494.                         log.info('OK, done.')
  495.                         sys.exit(0)
  496.                     
  497.                     if not fax_name or fax_name.lower().strip() == 'm':
  498.                         fax_name = fax_default_model
  499.                     
  500.                     name_ok = True
  501.                     for d in installed_print_devices.keys():
  502.                         for p in installed_print_devices[d]:
  503.                             if fax_name == p:
  504.                                 log.error('A print queue with that name already exists. Please enter a different name.')
  505.                                 name_ok = False
  506.                                 break
  507.                                 continue
  508.                         
  509.                     
  510.                     for d in installed_fax_devices.keys():
  511.                         for f in installed_fax_devices[d]:
  512.                             if fax_name == f:
  513.                                 log.error('A fax queue with that name already exists. Please enter a different name.')
  514.                                 name_ok = False
  515.                                 break
  516.                                 continue
  517.                         
  518.                     
  519.                     for c in fax_name:
  520.                         if c in (' ', '#', '/', '%'):
  521.                             log.error("Invalid character '%s' in fax name. Please enter a name that does not contain this character." % c)
  522.                             name_ok = False
  523.                             continue
  524.                     
  525.                     if name_ok:
  526.                         break
  527.                         continue
  528.             
  529.         else:
  530.             fax_name = fax_default_model
  531.         log.info('Using queue name: %s' % fax_name)
  532.         fax_type = mq.get('fax-type', FAX_TYPE_NONE)
  533.         if prop.hpcups_build:
  534.             if fax_type == FAX_TYPE_SOAP:
  535.                 fax_ppd_name = 'HP-Fax2-hpcups'
  536.             else:
  537.                 fax_ppd_name = 'HP-Fax-hpcups'
  538.         elif fax_type == FAX_TYPE_SOAP:
  539.             fax_ppd_name = 'HP-Fax2-hpijs'
  540.         else:
  541.             fax_ppd_name = 'HP-Fax-hpijs'
  542.         for f in ppds:
  543.             if f.find(fax_ppd_name) >= 0:
  544.                 fax_ppd = f
  545.                 log.debug('Found PDD file: %s' % fax_ppd)
  546.                 break
  547.                 continue
  548.         else:
  549.             sys.exit(1)
  550.         if auto:
  551.             (location, info) = ('', 'Automatically setup by HPLIP')
  552.         else:
  553.             while True:
  554.                 location = raw_input(log.bold('Enter a location description for this printer (q=quit) ?'))
  555.                 if location.strip().lower() == 'q':
  556.                     log.info('OK, done.')
  557.                     sys.exit(0)
  558.                 
  559.                 break
  560.             while True:
  561.                 info = raw_input(log.bold('Enter additonal information or notes for this printer (q=quit) ?'))
  562.                 if info.strip().lower() == 'q':
  563.                     log.info('OK, done.')
  564.                     sys.exit(0)
  565.                 
  566.                 break
  567.         log.info(log.bold('\nAdding fax queue to CUPS:'))
  568.         log.info('Device URI: %s' % fax_uri)
  569.         log.info('Queue name: %s' % fax_name)
  570.         log.info('PPD file: %s' % fax_ppd)
  571.         log.info('Location: %s' % location)
  572.         log.info('Information: %s' % info)
  573.         cups.setPasswordPrompt('You do not have permission to add a fax device.')
  574.         if not os.path.exists(fax_ppd):
  575.             (status, status_str) = cups.addPrinter(fax_name.encode('utf8'), fax_uri, location, '', fax_ppd, info)
  576.         else:
  577.             (status, status_str) = cups.addPrinter(fax_name.encode('utf8'), fax_uri, location, fax_ppd, '', info)
  578.         log.debug('addPrinter() returned (%d, %s)' % (status, status_str))
  579.         installed_fax_devices = device.getSupportedCUPSDevices([
  580.             'hpfax'])
  581.         log.debug(installed_fax_devices)
  582.         if fax_uri not in installed_fax_devices or fax_name not in installed_fax_devices[fax_uri]:
  583.             log.error('Fax queue setup failed. Please restart CUPS and try again.')
  584.             sys.exit(1)
  585.         
  586.         tui.header('FAX HEADER SETUP')
  587.         if auto:
  588.             setup_fax = False
  589.         else:
  590.             while True:
  591.                 user_input = raw_input(log.bold('\nWould you like to perform fax header setup (y=yes*, n=no, q=quit) ?')).strip().lower()
  592.                 if user_input == 'q':
  593.                     log.info('OK, done.')
  594.                     sys.exit(0)
  595.                 
  596.                 if not user_input:
  597.                     user_input = 'y'
  598.                 
  599.                 setup_fax = user_input == 'y'
  600.                 if user_input in ('y', 'n', 'q'):
  601.                     break
  602.                 
  603.                 log.error("Please enter 'y' or 'n'")
  604.         if setup_fax:
  605.             d = fax.getFaxDevice(fax_uri, disable_dbus = True)
  606.             
  607.             try:
  608.                 d.open()
  609.             except Error:
  610.                 log.error('Unable to communicate with the device. Please check the device and try again.')
  611.  
  612.             
  613.             try:
  614.                 tries = 0
  615.                 ok = True
  616.                 while True:
  617.                     tries += 1
  618.                     
  619.                     try:
  620.                         current_phone_num = str(d.getPhoneNum())
  621.                         current_station_name = str(d.getStationName())
  622.                     except Error:
  623.                         log.error('Could not communicate with device. Device may be busy. Please wait for retry...')
  624.                         time.sleep(5)
  625.                         ok = False
  626.                         if tries > 12:
  627.                             break
  628.                         
  629.                         tries > 12
  630.  
  631.                     ok = True
  632.                     break
  633.                 if ok:
  634.                     while True:
  635.                         if current_phone_num:
  636.                             phone_num = raw_input(log.bold("\nEnter the fax phone number for this device (c=use current:'%s'*, q=quit) ?" % current_phone_num))
  637.                         else:
  638.                             phone_num = raw_input(log.bold('\nEnter the fax phone number for this device (q=quit) ?'))
  639.                         if phone_num.strip().lower() == 'q':
  640.                             log.info('OK, done.')
  641.                             sys.exit(0)
  642.                         
  643.                         if current_phone_num:
  644.                             if not phone_num or phone_num.strip().lower() == 'c':
  645.                                 phone_num = current_phone_num
  646.                             
  647.                         if len(phone_num) > 50:
  648.                             log.error('Phone number length is too long (>50 characters). Please enter a shorter number.')
  649.                             continue
  650.                         
  651.                         ok = True
  652.                         for x in phone_num:
  653.                             if x not in '0123456789-(+) ':
  654.                                 log.error('Invalid characters in phone number. Please only use 0-9, -, (, +, and )')
  655.                                 ok = False
  656.                                 break
  657.                                 continue
  658.                         
  659.                         if not ok:
  660.                             continue
  661.                         
  662.                         break
  663.                     while True:
  664.                         if current_station_name:
  665.                             station_name = raw_input(log.bold("\nEnter the name and/or company for this device (c=use current:'%s'*, q=quit) ?" % current_station_name))
  666.                         else:
  667.                             station_name = raw_input(log.bold('\nEnter the name and/or company for this device (q=quit) ?'))
  668.                         if station_name.strip().lower() == 'q':
  669.                             log.info('OK, done.')
  670.                             sys.exit(0)
  671.                         
  672.                         if current_station_name:
  673.                             if not station_name or station_name.strip().lower() == 'c':
  674.                                 station_name = current_station_name
  675.                             
  676.                         if len(station_name) > 50:
  677.                             log.error('Name/company length is too long (>50 characters). Please enter a shorter name/company.')
  678.                             continue
  679.                         
  680.                         break
  681.                     
  682.                     try:
  683.                         d.setStationName(station_name)
  684.                         d.setPhoneNum(phone_num)
  685.                     except Error:
  686.                         log.error('Could not communicate with device. Device may be busy.')
  687.  
  688.                     log.info('\nParameters sent to device.')
  689.             finally:
  690.                 d.close()
  691.  
  692.         
  693.     
  694.     if setup_print:
  695.         print_test_page = False
  696.         tui.header('PRINTER TEST PAGE')
  697.         if auto:
  698.             if testpage_in_auto_mode:
  699.                 print_test_page = True
  700.             
  701.         else:
  702.             (ok, print_test_page) = tui.enter_yes_no('\nWould you like to print a test page')
  703.             if not ok:
  704.                 sys.exit(0)
  705.             
  706.         if print_test_page:
  707.             path = utils.which('hp-testpage')
  708.             if printer_name:
  709.                 param = '-p%s' % printer_name
  710.             else:
  711.                 param = '-d%s' % print_uri
  712.             if len(path) > 0:
  713.                 cmd = 'hp-testpage %s' % param
  714.             else:
  715.                 cmd = 'python ./testpage.py %s' % param
  716.             log.debug(cmd)
  717.             os.system(cmd)
  718.         
  719. except KeyboardInterrupt:
  720.     log.error('User exit')
  721.  
  722. log.info('')
  723. log.info('Done.')
  724.